package com.goodfather.textbook.youdao.pad.util;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;


/**
 * Created by lxj on 2016/5/23.
 */
public class ToastUtil {
    private static Toast toast;

    private static boolean isDebug = true;

    private final static int HANDLER_TOAST_LONG_MESSAGE = 10;
    private final static int HANDLER_TOAST_SHORT_MESSAGE = 20;
    private volatile static long preTimestamp;
    private volatile static String preContent = "";
    private static int TOAST_INTERVAL = 100;
    private static Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            String text = (String) msg.obj;
            switch (msg.what) {
                case HANDLER_TOAST_LONG_MESSAGE:
                    if (toast == null) {
                        //创建toast
                        toast = Toast.makeText(C.get(), text, Toast.LENGTH_LONG);
                    }
                    break;

                case HANDLER_TOAST_SHORT_MESSAGE:
                    if (toast == null) {
                        //创建toast
                        toast = Toast.makeText(C.get(), text, Toast.LENGTH_SHORT);
                    }
                    break;
            }

            //如果吐司已经创建，那么直接更改吐司的文本即可
            toast.setText(text);
            //最后显示
            toast.show();
        }
    };

    /**
     * 显示短时间toast
     *
     * @param text
     */
    public static void showShortToast(String text) {
        showMToast(text, HANDLER_TOAST_SHORT_MESSAGE);
    }

    /**
     * 抽象显示Toast公共方法
     *
     * @param text
     * @param handler
     * @author wuyuan
     * @datetime 20170712
     */
    private static void showMToast(final String text, final int handler) {
        if (C.get() != null) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    Message message = new Message();
                    message.what = handler;
                    message.obj = text;

                    mHandler.handleMessage(message);
                }
            });
        }
    }

    /**
     * 显示短时间toast
     *
     * @param text
     */
    public static void showShortToastDebug(String text) {
        if (isDebug) {
            if (toast == null) {
                //创建toast
                toast = Toast.makeText(C.get(), text, Toast.LENGTH_SHORT);
            }
            //如果吐司已经创建，那么直接更改吐司的文本即可
            toast.setText(text);
            //最后显示
            toast.show();
        }
    }

    /**
     * 显示长时间toast
     *
     * @param text
     */
    public static void showLongToast(String text) {
        showMToast(text, HANDLER_TOAST_LONG_MESSAGE);
    }


    /**
     * 显示自定义长度的toast
     *
     * @param text
     * @param duration
     */
    public static void showToast(String text, int duration) {
        if (toast == null) {
            //创建toast
            toast = Toast.makeText(C.get(), text, duration);
        }
        //如果吐司已经创建，那么直接更改吐司的文本即可
        toast.setText(text);
        //最后显示
        toast.show();
    }

    /**
     * 显示自定义toast位置
     * <p>
     * wuyuan 20170524
     *
     * @param text
     * @param duration
     * @param location
     */
    public static void showToastcation(String text, int duration, int location) {
        if (toast == null) {
            //创建toast
            toast = Toast.makeText(C.get(), text, duration);
        }
        //如果吐司已经创建，那么直接更改吐司的文本即可
        toast.setText(text);
        // 显示位置
        toast.setGravity(location, 0, 10);
        //最后显示
        toast.show();
    }

    public static void show(final String msg) {
        ThreadUtil.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                long t0 = System.currentTimeMillis();
                if (t0 - preTimestamp > TOAST_INTERVAL || !preContent.equals(msg)) {
                    Toast.makeText(C.get(), msg, Toast.LENGTH_SHORT).show();
                    preTimestamp = t0;
                    preContent = msg;
                }
            }
        });
    }

    public static void show(int strId) {
        String str = C.get().getString(strId);
        show(str);
    }
}
